﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class RC_InquireRequest : System.Web.UI.Page
    {
        private RadComboBox cmbYear, cmbJobSite, cmbJobSiteMove, cmbClass;
        private IMasEducationQualificationBLL qualBLL = new MasEducationQualificationBLL();
        private IMasEducationMajorBLL majBLL = new MasEducationMajorBLL();
        private IMasEducationInstituteBLL insBLL = new MasEducationInstituteBLL();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private IPositionTypeBLL posiTypeBLL = new PositionTypeBLL();
        private MoveRequestBLL moveReqBLL = new MoveRequestBLL();
        private InquireBLL inquirBLL = new InquireBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            IncludeJava();
            if (!IsPostBack)
            {
                bindYear();
                bindEmpType();
                bindJobSite();
            }
        }
        public void IncludeJava()
        {
            HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", "../../Scripts/jsfunction.js");
            Page.Header.Controls.Add(script);
        }
        private void bindYear()
        {
            cmbYear = (RadComboBox)ddlYear.FindControl("cbYear");
            ddlYear.ListYear();
            cmbYear.SelectedValue = Convert.ToString(Int32.Parse(DateTime.Today.Year.ToString()) + 543);
        }

        private void bindEmpType()
        {
            IList<EmployeeTypeInfo> infoList = empTypeBLL.ListEmployeeTypes();
            ddlEmployeeType.DataSource = infoList;
            ddlEmployeeType.DataBind();
        }
        protected void ddlEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            bindPositionType();
        }

        private void bindJobSite()
        {
            cmbJobSite = (RadComboBox)ddlJobSite.FindControl("cbJobSite");
            ddlJobSite.ListJobSiteComboBox(SystemInfo._JobsiteExternal);
            ddlJobSite.DataBind();

            cmbJobSiteMove = (RadComboBox)ddlJobSiteMove.FindControl("cbJobSite");
            ddlJobSiteMove.ListJobSiteComboBox(SystemInfo._JobsiteExternal);
            ddlJobSiteMove.DataBind();
        }

        private void bindPositionType()
        {
            IList<PositionTypeInfo> infoList = posiTypeBLL.ListPositionTypes(int.Parse(ddlEmployeeType.SelectedValue));
            ddlPosiType.DataSource = infoList;
            ddlPosiType.DataBind();
        }

        protected void ddlPosiType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            bindClass();
        }

        private void bindClass()
        {
            cmbClass = (RadComboBox)ddlClass.FindControl("cbClass");
            ddlClass.ListClassComboBox(ddlPosiType.SelectedValue);
            ddlClass.DataBind();
        }

        protected void btnFindQua_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string st = Hide.Value;
                if (st == "1")
                {
                    lbQuali.DataValueField = "Value";
                    lbQuali.DataTextField = "Text";
                    lbQuali.DataSource = (ArrayList)Session["arr"];
                    lbQuali.DataBind();

                    Session.Remove("arr");
                }
                else
                    lbQuali.Items.Clear();
            }
            catch
            {

            }
        }
        protected void btnFindQua_Load(object sender, EventArgs e)
        {
            btnFindQua.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogQualification.aspx'" + ",'../frmIframe.html',700,500)");
        }

        protected void btnFindMajor_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string st = Hide.Value;
                if (st == "1")
                {
                    lbMajor.DataValueField = "Value";
                    lbMajor.DataTextField = "Text";
                    lbMajor.DataSource = (ArrayList)Session["arr"];
                    lbMajor.DataBind();                    
                    Session.Remove("arr");
                }
            }
            catch
            {

            }
        }
        protected void btnFindMajor_Load(object sender, EventArgs e)
        {
            btnFindMajor.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogMajor.aspx'" + ",'../frmIframe.html',700,500)");
        }

        protected void btnFindIns_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string st = Hide.Value;
                if (st == "1")
                {
                    lbIns.DataValueField = "Value";
                    lbIns.DataTextField = "Text";
                    lbIns.DataSource = (ArrayList)Session["arr"];
                    lbIns.DataBind();

                    Session.Remove("arr");
                }
            }
            catch
            { 
            
            }
        }
        protected void btnFindIns_Load(object sender, EventArgs e)
        {
            btnFindIns.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogIns.aspx'" + ",'../frmIframe.html',700,500)");
        }

        protected void btnMoveQua_Click(object sender, ImageClickEventArgs e)
        {
           // lbQuali.Items.
            for (int i = lbQuali.Items.Count - 1; i >= 0; i--)
            {
                if (lbQuali.Items[i].Selected)
                {
                    lbQuali.Items.Remove(lbQuali.Items[i]);
                }
            }
        }

        protected void btnMoveMajor_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = lbMajor.Items.Count - 1; i >= 0; i--)
            {
                if (lbMajor.Items[i].Selected)
                {
                    lbMajor.Items.Remove(lbMajor.Items[i]);
                }
            }
        }

        protected void btnMoveIns_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = lbIns.Items.Count - 1; i >= 0; i--)
            {
                if (lbIns.Items[i].Selected)
                {
                    lbIns.Items.Remove(lbIns.Items[i]);
                }
            }
        }
      //  ./App_UI/Forms/RC_InquireRequest.aspx
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Forms/RC_InquireRequest.aspx");
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string lbQualiStr="", lbMajorStr="", lbInsStr="";
            for (int i = 0; i < lbQuali.Items.Count; i++)
            {
                lbQualiStr += lbQuali.Items[i].Value + ",";
            }
            lbQualiStr = lbQualiStr != "" ? lbQualiStr.Remove(lbQualiStr.Length - 1) : "";

            for (int i = 0; i < lbMajor.Items.Count; i++)
            {
                lbMajorStr += lbMajor.Items[i].Value + ",";
            }
            lbMajorStr = lbMajorStr != "" ? lbMajorStr.Remove(lbMajorStr.Length - 1) : "";

            for (int i = 0; i < lbIns.Items.Count; i++)
            {
                lbInsStr += lbIns.Items[i].Value + ",";
            }
            lbInsStr = lbInsStr != "" ? lbInsStr.Remove(lbInsStr.Length - 1) : "";

            cmbYear = (RadComboBox)ddlYear.FindControl("cbYear");
            cmbJobSite = (RadComboBox)ddlJobSite.FindControl("cbJobSite");
            cmbJobSiteMove = (RadComboBox)ddlJobSiteMove.FindControl("cbJobSite");
            cmbClass = (RadComboBox)ddlClass.FindControl("cbClass");

            DataTable dt = new DataTable();

            if (ddlFilterType.SelectedIndex == 0)
            {
                lblTitle.Text = "รายการคำร้องขอย้าย";
                dt = moveReqBLL.listMoveRequestMaxDate(int.Parse(cmbYear.SelectedValue)
                        , ddlEmployeeType.SelectedValue
                        , ddlPosiType.SelectedValue
                        , cmbClass.SelectedValue
                        , lbQualiStr
                        , lbMajorStr
                        , lbInsStr
                        , cmbJobSite.SelectedValue
                        , cmbJobSiteMove.SelectedValue);
                grdMovResult.DataSource = dt;
                grdMovResult.DataBind();
                grdMovResult.Visible = true;
                grdInquiresResult.Visible = false;
            }
            else
            {
                lblTitle.Text = "แบบแสดงความจำนง";
                dt = inquirBLL.ListInquire(int.Parse(cmbYear.SelectedValue)
                        , ddlEmployeeType.SelectedValue
                        , ddlPosiType.SelectedValue
                        , cmbClass.SelectedValue
                        , lbQualiStr
                        , lbMajorStr
                        , lbInsStr
                        , cmbJobSite.SelectedValue
                        , cmbJobSiteMove.SelectedValue);
                grdInquiresResult.DataSource = dt;
                grdInquiresResult.DataBind();
                grdInquiresResult.Visible = true;
                grdMovResult.Visible = false;
            }            
            Session["listData"] = dt;
        }

        protected void grdMovResult_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "ExpandCollapse")
                return;
            grdMovResult.DataSource = (DataTable)Session["listData"];
            grdMovResult.DataBind();
        }

        protected void grdInquiresResult_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "ExpandCollapse")
                return;
            grdInquiresResult.DataSource = (DataTable)Session["listData"];
            grdInquiresResult.DataBind();
        }

    }
}
